from django.http import HttpResponse, HttpResponseNotFound, JsonResponse, StreamingHttpResponse, FileResponse
from django.shortcuts import render, redirect


# Create your views here.
def index(request):
    return render(request, 'http.html')
    # return redirect("/blog/2")
    # content_value = {"msg": "学Python，上www.python222.com"}
    # return render(request, 'index.html', context=content_value)

    # return JsonResponse({'foo': 'bar'})
    # return HttpResponseNotFound()
    # html = "<font color='red'>学Python，上www.python222.com</font>"
    # return HttpResponse(html, status=200)


def blog(request, id):
    if id == 0:
        return redirect("/s2/error.html")
    else:
        return HttpResponse("id是" + str(id) + "的博客页面")


def blog2(request, year, month, day, id):
    return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + '  id是' + str(id) + "的博客页面")


def blog3(request, year, month, day):
    return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + "的博客页面")


# 定义文件路径
file_path = "D:\\360zip_setup.exe"


def download_file1(request):
    file = open(file_path, "rb")  # 打开文件
    response = HttpResponse(file)  # 创建HttpResponse对象
    response['Content_Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename=file1.exe'
    return response


def download_file2(request):
    file = open(file_path, "rb")  # 打开文件
    response = StreamingHttpResponse(file)  # 创建StreamingHttpResponse对象
    response['Content_Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename=file2.exe'
    return response


def download_file3(request):
    file = open(file_path, "rb")  # 打开文件
    response = FileResponse(file)  # 创建FileResponse对象
    response['Content_Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename=file3.exe'
    return response


def get_test(request):
    """
    get请求测试
    :param request:
    :return:
    """
    print(request.method)  # 请求方式
    # 常用属性
    print(request.content_type)
    print(request.content_params)
    print(request.COOKIES)
    print(request.scheme)
    # 常用方法
    print(request.is_secure())
    print(request.get_host())
    print(request.get_full_path())

    print(request.GET.get("name"))
    print(request.GET.get("pwd"))
    print(request.GET.get("aaaa", "666"))

    return HttpResponse("http get ok")


def post_test(request):
    """
    post请求参数
    :param request:
    :return:
    """
    print(request.method)
    print(request.POST.get("name"))
    print(request.POST.get("pwd"))
    print(request.POST.get("aaaa", "666"))
    return HttpResponse("http post ok")


def to_login(request):
    """
    跳转登录页面
    :param request:
    :return:
    """
    return render(request, "login.html")


def login(request):
    """
    登录
    :param request:
    :return:
    """
    user_name = request.POST.get("user_name")
    pwd = request.POST.get("pwd")
    if user_name == 'python222' and pwd == "123456":
        request.session['currentUserName'] = user_name  # session中存一个用户名
        print("session获取", request.session['currentUserName'])
        response = render(request, "main.html")
        response.set_cookie("rememeber_me", True)  # 设置cookie
        return response
    else:
        context_value = {"error_info": '用户名或者密码错误'}
        return render(request, "login.html", context=context_value)
